/* Timestamp formatting routines.
 *
 * Copyright 2008 Cian Synnott.
 * 
 * $Id: time_format.c 19 2009-11-27 20:46:48Z cian $
 */
#include <stdlib.h>
#include <time.h>

#include "log.h"

/* See time_format.h */
#define TIME_BUFFER_LEN 128
char *time_formatted_by(char *fmt, int global) {
  struct tm *timespec;
  time_t time_now;
  char *string;

  string = (char *) calloc(TIME_BUFFER_LEN, sizeof(char));
  if (string == NULL) return NULL;

  time_now = time(NULL);
  if (time_now == -1) return NULL;
  
  timespec = global ? gmtime(&time_now) : localtime(&time_now); 
  if (timespec == NULL) return NULL;

  strftime(string, TIME_BUFFER_LEN, fmt, timespec);

  return string;  
}
